package leetcode_1_200;

public class LeeCode_96 {
    public static void main(String[] args) {
        System.out.println(numTrees(3));
        System.out.println(numTrees2(3));
    }
    private static int numTrees(int n) {
        return search(1, n);
    }
    private static int numTrees2(int n) {
        int[] G = new int[n + 1];
        G[0] = 1;
        G[1] = 1;
        for (int i = 2; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                G[i] += G[j - 1] * G[i - j];
            }
        }
        return G[n];
    }
    private static int search(int start, int end){
        int res = 0;
        if (start > end)
            return 0;
        if (start == end){
            return 1;
        }
        for (int i = start; i <= end; i++) {
            int l = search(start, i - 1);
            int r = search(i + 1, end);
            if (l == 0 || r == 0){
                res += l == 0 ? r : l;
            }else
                res += l * r;
        }
        return res;
    }
}
